﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using BYFW.DAL;
using LDSP.Helper;

namespace LDSP.UI.EnterpriseAdmin
{
    public partial class UserDownloadReport : BRL.BaseEnterpriseAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (this.RB())
                {
                    if (!IsPostBack)
                    {
                        this.InitData();
                        this.GetCondition();
                        this.BindGrid();
                    }
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        private int col_idx = 0;
        private int col_enterprise = 1;
        private int col_user = 2;
        private int col_codification = 3;
        private int col_downloadDT = 4;


        /// <summary>
        /// 传入参数，设置过滤条件
        /// </summary>
        protected void SetSpecialPageInfo()
        {
            g_pageInfo.Direction = true;
            g_pageInfo.Sort = "downloadDT";
            g_pageInfo.PageSize = GetPageSizeSetting();
            g_pageInfo.PageIndex = this.grd_list.PageIndex;
        }
        protected bool RB()
        {
            return this.AllowEnterpriseUser();
        }

        protected void InitData()
        {
            //不缓存则清空页面信息
            if (!needCachePageInfo)
            {
                g_pageInfo.ClearPageInfo();
            }
            this.SetSpecialPageInfo();

            this.BindDDLDownloadRank();
            this.BindDDLPeriod();
            this.BindDDLPageSize();

            if (IsEnterpriseGroupAdmin)
            {
                this.BindDDLSubdiary();
            }

            this.SetStatus();
        }

        private void SetStatus()
        {
            this.grd_list.Columns[col_enterprise].Visible = IsEnterpriseGroupAdmin;
            this.tr_subdiary.Visible = IsEnterpriseGroupAdmin;
        }
        protected void BindDDLDownloadRank()
        {
            this.ddl_downloadRank.Items.Clear();
            this.ddl_downloadRank.Items.Add("10");
            this.ddl_downloadRank.Items.Add("20");
            this.ddl_downloadRank.Items.Add("50");
            this.ddl_downloadRank.SelectedIndex = 0;
        }
        protected void BindDDLPeriod()
        {
            this.ddl_period.Items.Clear();
            this.ddl_period.Items.Add("一周");
            this.ddl_period.Items.Add("一月");
            this.ddl_period.Items.Add("一季");
            this.ddl_period.Items.Add("一年");
            this.ddl_period.SelectedIndex = 0;
        }
        protected void BindDDLPageSize()
        {
            CommonControlInitial.InitialPageSize(this.ddl_pageSize);
            this.ddl_pageSize.SelectedValue = g_pageInfo.PageSize.ToString();
        }
        protected void BindDDLSubdiary()
        {
            CommonControlInitial.InitialSubdiary(this.ddl_subdiary, this.LoginUser.Enterprise.GetSubsidiaryList());
        }

        protected void BindGrid()
        {
            DataTable dt = BYFW.SFL.CommonOP.SortedTable(new DownloadLog().GetRecordsByCond(g_pageInfo.ConditionWhere), g_pageInfo.GetSortExpression());
            this.grd_list.PageSize = g_pageInfo.PageSize;
            this.grd_list.PageIndex = g_pageInfo.PageIndex;
            this.grd_list.DataSource = dt;
            this.grd_list.DataBind();

            SavePageSizeSetting();

            this.TotalRecords.Text = LDSP.Helper.StringUtil.GetSearchText(dt.Rows.Count);
        }
        protected void GetCondition()
        {
            string cond = "";

            if (IsEnterpriseGroupAdmin)
            {
                string subdiaries = this.LoginUser.EnterpriseID + ";";
                string enterpriseId = this.ddl_subdiary.SelectedValue;

                if (enterpriseId == "0")
                {
                    //如果是集团用户，则搜索所有集团用户及子公司用户
                    IList<Enterprise> enterprises = this.LoginUser.Enterprise.GetSubsidiaryList();
                    foreach (Enterprise en in enterprises)
                    {
                        subdiaries += en.ID + ";";
                    }
                }
                else
                {
                    subdiaries += enterpriseId + ";";
                }

                cond = this.AppendCondition(cond, DefaultDB.GetSQLInAndNotInCondition("enterpriseID", EDataType.Int, subdiaries.ToString(), true, false), true);
            }
            else
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("enterpriseID", EDataType.Int, this.LoginUser.EnterpriseID.ToString(), "=", true), true);
            }

            cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("downloadDT", EDataType.Datetime, this.GetStartDT(), ">=", true), true);
            cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("downloadDT", EDataType.Datetime, DateTime.Now.ToString(), "<=", true), true);

            g_pageInfo.Condition = cond;
        }

        protected int GetTopNumber()
        {
            return this.GetInt(this.ddl_downloadRank.SelectedValue);
        }
        protected string GetStartDT()
        {
            DateTime res = DateTime.Now;
            switch (this.ddl_period.SelectedIndex)
            {
                case 0:
                    res = res.AddDays(-7);
                    break;
                case 1:
                    res = res.AddMonths(-1);
                    break;
                case 2:
                    res = res.AddMonths(-3);
                    break;
                case 3:
                    res = res.AddYears(-1);
                    break;
            }
            return this.FormatDT(res);
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            try
            {
                this.GetCondition();
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DownloadLog ett = new DownloadLog();
                    ett.GetProperties(((DataRowView)e.Row.DataItem).Row);
                    this.ShowSequenceNumber(e.Row, 0);

                    e.Row.Cells[col_downloadDT].Text = this.FormatDT(ett.DownloadDT);
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                g_pageInfo.PageIndex = e.NewPageIndex;
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                if (g_pageInfo.Sort == e.SortExpression)
                {
                    g_pageInfo.Direction = !g_pageInfo.Direction;
                }
                else
                {
                    g_pageInfo.Direction = true;
                }
                g_pageInfo.Sort = e.SortExpression;
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void ddl_pageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                g_pageInfo.PageSize = this.GetInt(this.ddl_pageSize.SelectedValue);
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
    }

}